package com.cyd.dialog;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import android.content.Context;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.cyd.base.APPBASE;
import com.cyd.homefamily.R;
import com.cyd.util.CityDataBase;
import com.gitonway.niftydialogeffects.widget.niftydialogeffects.Effectstype;
import com.gitonway.niftydialogeffects.widget.niftydialogeffects.NiftyDialogBuilder;

/**
 * @auto:陈梓斌
 * @version:1.0
 * @Create Time:Apr 1, 2015 5:35:19 PM
 * @Email:598967359@qq.com
 * @Todo:信息更新
 */
public class PersonalInformationDialog extends NiftyDialogBuilder implements
		OnCheckedChangeListener, android.view.View.OnClickListener,
		OnWheelChangedListener {

	private OnInformationUpdateListener mOnInformationUpdateListener;
	public RadioGroup mSexGroup;
	private Context mContext;
	private EditText mNameEdit;

	private WheelView mProvince;
	private WheelView mCity;
	private WheelView mCounty;
	private String[] provinces;
	private String[] citys;
	private String[] countys;
	private String provinceName;
	private String cityName;
	private String countyName;
	private CityDataBase database;

	private TYPE mType;

	public enum TYPE {
		NAME, SEX, LIVENOW, ADDRESS
	}

	public PersonalInformationDialog(Context context, ViewGroup parent, TYPE type) {
		super(context, R.style.dialog_untran);
		this.mContext = context;
		this.mType = type;
		database = new CityDataBase(mContext);
		getInstance(mContext);
		this.withEffect(Effectstype.RotateLeft);
		switch (type) {
		case SEX:
			initSex(parent);
			break;
		case NAME:
			initName();
			break;
		case ADDRESS:
		case LIVENOW:
			initWheel(parent);
			break;
		}
	}

	// 初始化名字对话框
	private void initName() {
		mNameEdit = new EditText(mContext);
		mNameEdit.setInputType(InputType.TYPE_CLASS_TEXT);
		mNameEdit.setBackgroundResource(R.drawable.shape_dialog_name);
		this.withTitle("修改姓名").withTitleColor("#F0F0F0").withMessage(null)
				.setCustomView(mNameEdit, mContext)
				.withMessageColor("#FFE74C3C").withDuration(400)
				.withButton1Text("取消").withButton2Text("确定")
				.setButton1Click(this).setButton2Click(this);
	}

	// 设置记录的名字
	public void setNameEdt(String str) {
		mNameEdit.setText(str);
	}

	// 初始化性别对话框
	private void initSex(ViewGroup parent) {
		View v = LayoutInflater.from(mContext).inflate(R.layout.dialog_sex,
				parent);
		mSexGroup = (RadioGroup) v.findViewById(R.id.sexGroup);
		mSexGroup.setOnCheckedChangeListener(this);
		this.withTitle("性别").withTitleColor("#F0F0F0").withMessage(null)
				.withMessageColor("#FFE74C3C").withDuration(400)
				.setCustomView(v, mContext);
	}

	private void initWheel(ViewGroup parent) {
		View v = LayoutInflater.from(mContext).inflate(R.layout.dialog_live,
				parent);
		mProvince = (WheelView) v.findViewById(R.id.wheel_province);
		mCity = (WheelView) v.findViewById(R.id.wheel_city);
		mCounty = (WheelView) v.findViewById(R.id.wheel_county);

		mProvince.addChangingListener(this);
		mCity.addChangingListener(this);
		mCounty.addChangingListener(this);

		initWheelViewBg();
		initProvinces();
		this.withTitle("现居地").withTitleColor("#F0F0F0").withMessage(null)
				.withMessageColor("#FFE74C3C").withDuration(400)
				.setButton1Click(this).setButton2Click(this)
				.withButton1Text("取消").withButton2Text("确定")
				.setCustomView(v, mContext);
	}

	private void initWheelViewBg() {
		mProvince.setShadowColor(
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red));
		mProvince.setWheelBackground(R.drawable.wheel_bg);
		mCity.setShadowColor(
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red));
		mCity.setWheelBackground(R.drawable.wheel_bg);
		mCounty.setShadowColor(
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red),
				getContext().getResources().getColor(R.color.dialog_msg_red));
		mCounty.setWheelBackground(R.drawable.wheel_bg);
	}

	private void initProvinces() {
		provinces = database.getPCC(APPBASE.FINDPROVINCE, "name");
		mProvince.setViewAdapter(new ArrayWheelAdapter<String>(mContext,
				provinces));
		upDateCitys();
		upDateCountys();
	}

	private void upDateCitys() {
		// TODO Auto-generated method stub
		int index = mProvince.getCurrentItem();
		provinceName = provinces[index];
		String sql = "select city from china_city_code where _id in(select max(_id) from china_city_code group by city ) and province ='"
				+ provinceName + "'";
		citys = database.getPCC(sql, "city");
		mCity.setViewAdapter(new ArrayWheelAdapter<String>(mContext, citys));
		mCity.setCurrentItem(0);
		upDateCountys();
	}

	private void upDateCountys() {
		// TODO Auto-generated method stub
		int index = mCity.getCurrentItem();
		cityName = citys[index];
		String sql = "select county from china_city_code where city ='"
				+ cityName + "'";
		countys = database.getPCC(sql, "county");
		mCounty.setViewAdapter(new ArrayWheelAdapter<String>(mContext, countys));
		mCounty.setCurrentItem(0);
	}

	// 回调单选按钮
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {

		if (mOnInformationUpdateListener != null) {
			RadioButton button = (RadioButton) group.findViewById(checkedId);
			mOnInformationUpdateListener.onInformationUpdate(button.getText()
					.toString(), TYPE.SEX);
			this.dismiss();
		}
	}

	// 点击事件
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub
		int id = v.getId();
		if (mOnInformationUpdateListener != null)
			if (id == R.id.button2) {
				switch (mType) {
				case NAME:
					mOnInformationUpdateListener.onInformationUpdate(mNameEdit
							.getText().toString(), TYPE.NAME);
					break;
				case LIVENOW:
					mOnInformationUpdateListener.onInformationUpdate(
							provinceName + "-" + cityName + "-" + countyName,
							TYPE.LIVENOW);
					break;
				case ADDRESS:
					mOnInformationUpdateListener.onInformationUpdate(
							provinceName + "-" + cityName + "-" + countyName,
							TYPE.ADDRESS);
					break;
				}
			}
		dismiss();
	}

	// 设置接口
	public interface OnInformationUpdateListener {
		public void onInformationUpdate(String str, TYPE type);
	}

	// 接口
	public void setOnInformationUpdateListener(
			OnInformationUpdateListener listener) {
		this.mOnInformationUpdateListener = listener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		if (wheel == mProvince)
			upDateCitys();
		else if (wheel == mCity)
			upDateCountys();
		countyName = countys[mCounty.getCurrentItem()];
	}
}
